#include "EbnGoal.h"
#include "EbnWorldState.h"
#include "EbnCondition.h"

CEbnGoal::CEbnGoal(CEbnWorldState * worldState, bool protect, bool positive)
{
    mpCondition = new CEbnCondition(worldState, positive);
    mImportance = 1;
    mProtected = protect;
}

CEbnGoal::~CEbnGoal(void)
{
    mFromLinkList.clear();

    if (mpCondition)
    {
        delete mpCondition;
        mpCondition = NULL;
    }
}

float CEbnGoal::getImportance(void)
{
    return mImportance;
}

void  CEbnGoal::setImportance(float importance)
{
    mImportance = importance;
}

bool CEbnGoal::isProtected(void)
{
    return mProtected;
}

void CEbnGoal::setProtected(bool protect)
{
    mProtected = protect;
}

CEbnCondition * CEbnGoal::getCondition(void)
{
    return mpCondition;
}

bool CEbnGoal::isReached(void)
{
    if (mpCondition)
    {
        if (1 == mpCondition->getValue())
        {
            return true;
        }
        else
        {
            return false;
        }

    }

    return true;
}

CEbnCondition * CEbnGoal::getRelevanceCondition(void)
{
    return mpRelevanceCondition;
}

void CEbnGoal::setRelevanceCondition(CEbnCondition * pCondition)
{
    mpRelevanceCondition = pCondition;
}


bool CEbnGoal::hasFromLink(CEbnLink * pLink)
{
    vector<CEbnLink *>::iterator itLink;
    for (itLink = mFromLinkList.begin(); itLink != mFromLinkList.end(); itLink++)
    {
        if ((*itLink) == pLink)
        {
            return true;
        }
    }

    return false;
}

void CEbnGoal::addFromLink(CEbnLink * pLink)
{
    bool exist = hasFromLink(pLink);

    if (exist == false)
    {
        mFromLinkList.push_back(pLink);
    }
}

int CEbnGoal::getFromLinkCount(void)
{
    return (int)mFromLinkList.size();
}

CEbnLink * CEbnGoal::getFromLink(int index)
{
    if ((index < 1) || (index > getFromLinkCount()))
    {
        return NULL;
    }

    return mFromLinkList.at(index - 1);
}

bool CEbnGoal::hasWorldState(CEbnWorldState * worldState)
{
    if (worldState == mpCondition->getWorldState())
    {
        return true;
    }

    return false;
}
